package com.dinggao.crossgo.module.roadbook.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.dinggao.crossgo.core.MainApplication;
import com.dinggao.crossgo.core.constant.CommonConstant;
import com.dinggao.crossgo.core.dao.entity.Path;
import com.dinggao.crossgo.core.util.SharedPreferencesUtil;

public class LocationService extends Service {

    private static final String TAG = "LocationService";

    public static final String ACTION_UPDATE_LOCATION = "action.update.location";
    //声明AMapLocationClient类对象
    AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "start LocationService!");

        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果：
        mLocationOption.setInterval(3*1000);
        getPosition();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    public void getPosition(){
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener(){

        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if(amapLocation == null){
                Log.i(TAG, "amapLocation is null!");
                return;
            }
            if(amapLocation.getErrorCode()!=0){
                Log.i(TAG, "amapLocation has exception errorCode:" + amapLocation.getErrorCode());
                return;
            }

            Double longitude = amapLocation.getLongitude();//获取经度
            Double latitude = amapLocation.getLatitude();//获取纬度
            String longitudeStr = String.valueOf(longitude);
            String latitudeStr = String.valueOf(latitude);

            String localRoadBookId = SharedPreferencesUtil.getSharedPreferencesString(LocationService.this,
                    CommonConstant.USERINFO_SHREDPREFERENCE,
                    CommonConstant.USRERINFO_LOCAL_ROADBOOK_ID);

            if (!TextUtils.isEmpty(localRoadBookId)
                    && !TextUtils.isEmpty(longitudeStr)
                    && !TextUtils.isEmpty(latitudeStr)) {
                // 将坐标记入本地数据库
                Path path = new Path();
                path.setRoadBookId(Long.valueOf(localRoadBookId));
                path.setLat(latitude);
                path.setLng(longitude);
                MainApplication.getDaoInstant().getPathDao().insert(path);

                // 发送广播首页地图
                Intent intent = new Intent();
                intent.setAction(ACTION_UPDATE_LOCATION);
                intent.putExtra("longitude", longitudeStr);
                intent.putExtra("latitude", latitudeStr);
                sendBroadcast(intent);
                Log.i(TAG, "定位坐标是:" + longitudeStr + "  " + latitudeStr);
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocationClient.stopLocation();
        mLocationClient.onDestroy();
        mLocationOption = null;
        mLocationClient = null;
    }
}